import React, { Component } from 'react'
import moment from 'moment';

export default class EleClock extends Component {
  //声明状态
  state = {
    timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
  }
  render() {
    return (
      <div style={{border: 'solid 1px #345', padding: 20, display: 'inline-block'}}>
        {/* 使用状态 */}
        {this.state.timeStr}
      </div>
    )
  }

  //组件挂载完毕之后的回调  
  componentDidMount(){
    //定时器
    this.timer = setInterval(() => {
      console.log('run')
      //更新状态
      this.setState({
        timeStr: moment().format('YYYY-MM-DD HH:mm:ss')
      })
    }, 1000)
  }

  //组件卸载之前的回调
  componentWillUnmount(){
    //清除定时器
    clearInterval(this.timer);
  }
}
